package com.wzt.lianfirecontrol.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.utils.SimpleCache;
import com.xh.common.thread.AsyncExecutor;

import java.util.ArrayList;
import java.util.List;
/*
* 闪屏界面
* */
public class WelcomeActivity extends BaseActivity {

    private final static int REQUEST_REQUEST_CODE = 0;

    private int requestTime = 0;

    /**
     * 需要向用户申请的权限列表
     */
    private static String[] permissions = new String[]{Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 清除更新方面的数据
        SimpleCache.clearDataForOneDay(this, ConstData.UPDATE_DIALOG_SHOW);
        setFullScreen();
        setContentView(R.layout.a_welcome);
        initApp();

    }

    private void initApp() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            // 跳转页面
            skipToNextPage();
        } else {
            // 检查权限
            checkPermissions();
        }
    }

    /**
     * 申请权限
     */
    private void checkPermissions() {
        try {
            if (requestTime > 2){
                finish();
            }
            requestTime++;
            List<String> permissionList = new ArrayList<>();
            for (String permission : permissions) {
                if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, permission)) {
                    permissionList.add(permission);
                }
            }
            if (permissionList.size() > 0) {
                ActivityCompat.requestPermissions(this, permissionList.toArray(new String[]{}), REQUEST_REQUEST_CODE);
                return;
            }
            permissionNextTips();

        } catch (Throwable e) {
            Log.e("checkPermissions", "", e);
        }
    }

    private void permissionNextTips() {
        skipToNextPage();
    }

    private void skipToNextPage(){
        AsyncExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1500);
                    // 判断用户是否登录
                    if (UserInfoModel.isLogin(WelcomeActivity.this)){
                        openActivity(MainActivity.class);
                    }else {
                        openActivity(LoginActivity.class);
                    }
                    WelcomeActivity.this.finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_REQUEST_CODE:
                // 到这里就表示用户允许了本次请求，我们继续检查是否还有待申请的权限没有申请
                if (isAllRequestedPermissionGranted()) {
                    permissionNextTips();
                } else {
                    checkPermissions();
                }
                break;
        }
    }

    private boolean isAllRequestedPermissionGranted() {
        for (final String permission : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, permission)) {
                return false;
            }
        }
        return true;
    }

    // 取消返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME)) {
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

}
